package common;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.laoxinwen.app.BuildConfig;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import bean.wraper.BaseWrapper;

public class HttpHelper {

    public static final String TAG_RESPONSE = "HttpResponse";

    public static <T extends BaseWrapper> AsyncHttpClient executePost(final Class<T> c, String url, final HttpCallBack<T> callBack) {
        return executePost(c, url, null, callBack);
    }

    public static <T extends BaseWrapper> AsyncHttpClient executePost(final Class<T> c, final String url, RequestParams params, final HttpCallBack<T> callBack) {
        AsyncHttpClient httpClient = new AsyncHttpClient();
        httpClient.addHeader("LXWAUTH", "nixiangkanlaoxinwen?shide!");
        httpClient.addHeader("LXWTOKEN", UserConfig.getRequestToken());
        httpClient.post(url, params, new TextHttpResponseHandler() {

            @Override
            public void onFailure(int resultCode, Header[] headers, String s, Throwable throwable) {
                Log.e(TAG_RESPONSE, "FAIL:" + url);
            }

            @Override
            public void onSuccess(int resultCode, Header[] headers, String content) {

                if (BuildConfig.DEBUG) {
                    Log.i(TAG_RESPONSE, content);
                }
                T t = null;
                try {
                    t = JSONObject.parseObject(content, c);
                } catch (Exception e) {
                    //                    ToastUtil.showLongToast("URL:"+url+"\nErrorMsg:"+e.getMessage());
                    Log.e("HttpHelper", "URL:" + url + "\nErrorMsg:" + e.getMessage());
                    callBack.onServerError(t);
                }
                if (null == t) {
                    Log.e(TAG_RESPONSE, content);
                    return;
                }
                if (null == t.getError()) {
                    callBack.onSucceed(t);
                } else {
                    callBack.onServerError(t);
                }
            }
        });
        return httpClient;
    }

    public static <T extends BaseWrapper> AsyncHttpClient executeGet(final Class<T> c, String url, final HttpCallBack<T> callBack) {
        return executeGet(c, url, null, callBack);
    }

    public static <T extends BaseWrapper> AsyncHttpClient executeGet(final Class<T> c, String url, RequestParams params, final HttpCallBack<T> callBack) {
        AsyncHttpClient httpClientient = new AsyncHttpClient();
        httpClientient.addHeader("LXWAUTH", "nixiangkanlaoxinwen?shide!");
        httpClientient.addHeader("LXWTOKEN", UserConfig.getRequestToken());
        httpClientient.get(url, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int resultCode, Header[] headers, String s, Throwable throwable) {
                callBack.onNetError();
            }

            @Override
            public void onSuccess(int resultCode, Header[] headers, String content) {
                if (BuildConfig.DEBUG) {
                    Log.i(TAG_RESPONSE, content);
                }
                T t = null;
                try {
                    t = JSONObject.parseObject(content, c);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (null == t) {
                    Log.e(TAG_RESPONSE, content);
                    return;
                }
                if (null == t.getError()) {
                    callBack.onSucceed(t);
                } else {
                    callBack.onServerError(t);
                }
            }
        });
        return httpClientient;
    }

}

